package cn.fl.customer.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstCorpHisService;
import cn.fl.customer.transform.CstCorpHisVOTransform;
import cn.fl.customer.vo.CstCorpHisVO;
import fl.customer.facade.customer.api.ICustomerAddApiService;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.api.ICustomerModifyApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.api.ICustomerRemoveApiService;
import fl.customer.facade.customer.condition.CstCorpHisQueryCondition;
import fl.customer.facade.customer.dto.CstCorpHisDTO;

@Service
public class CstCorpHisServiceImpl implements ICstCorpHisService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CstCorpHisServiceImpl.class);
	
	/**
	 *  客户API服务
	 */
	@Autowired
	private ICustomerApiService customerApiService;
	
	@Autowired
	private ICustomerAddApiService customerAddApiService;
	
	@Autowired
	private ICustomerModifyApiService customerModifyApiService;
	
	@Autowired
	private ICustomerRemoveApiService customerRemoveApiService;
	
	@Autowired
	private ICustomerQueryApiService customerQueryApiService;

	@Override
	public CstCorpHisVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "主键不能为空!");
		}
		CstCorpHisDTO dto = this.customerQueryApiService.selectCstCorpHisById(id);
		return CstCorpHisVOTransform.toVO(dto);
	}

	@Override
	public List<CstCorpHisVO> searchList(CstCorpHisQueryCondition qc)  throws BaseException{
		if(null == qc){
			BaseLogUtils.info(logger, "searchList", "查询对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询对象不能为空!");
		}
		
		if(StringUtils.isEmpty(qc.getCstId())){
			BaseLogUtils.info(logger, "searchList", "客户主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "客户主键不能为空!");
		}
		return CstCorpHisVOTransform.toVOList(customerApiService.searchCstCorpHisList(qc));
	}

	@Override
	public Long add(CstCorpHisVO entity)  throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "对象不能为空!");
		};
		return this.customerAddApiService.addCorpHis(CstCorpHisVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id)  throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "主键不能为空!");
		};
		return this.customerRemoveApiService.removeCstCorpHisById(id);
	}

	@Override
	public boolean modify(CstCorpHisVO entity)  throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "对象不能为空!");
		};
		
		if(StringUtils.isEmpty(entity.getId())){
			BaseLogUtils.info(logger, "modify", "主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "主键不能为空!");
		}
		return this.customerModifyApiService.modifyCstCorpHis(CstCorpHisVOTransform.toDTO(entity));
	}
}
